948A - Protect Sheep - CodeForces Solution


brute force dfs and similar graphs implementation *900

Please click on ads to support us..

Python Code:

def solution():
    rows,columnns=map(int, input().split())
    area=[]
    for i in range(rows):
        row= list(input())
        area.append(row)

    for i in range(rows):
        for j in range(columnns):
            if area[i][j]=="S":
                if  ((i<rows-1) and area[i+1][j]=="W"):
                    print("No", end="")
                    return
                elif  ((i<rows-1) and area[i+1][j]=="."):
                    area[i+1][j]="D"
                if (i>0 and area[i-1][j]=="W"):
                    print("No", end="")
                    return
                elif ((i>0) and area[i-1][j]=="."):
                    area[i-1][j]="D"
                if ((j<columnns-1) and area[i][j+1]=="W"):
                    print("No", end="")
                    return
                elif ((j<columnns-1) and area[i][j+1]=="."):
                    area[i][j+1]="D"
                if ((j>0) and area[i][j-1]=="W"):
                    print("No", end="")
                    return
                elif ((j>0) and area[i][j-1]=="."):
                    area[i][j-1]="D"
    print("Yes")       
    for i in range(rows):
        for j in range(columnns):
            print(area[i][j], end="")
        if i< rows-1:
            print()
            
solution()
 	 			      		 	 	    	 	 	 	 	

C++ Code:

// Problem: A. Protect Sheep
// Contest: Codeforces - Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)
// URL: https://codeforces.com/problemset/problem/948/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int r,c;
    
    cin>>r>>c;
    string a[r];
    for (long i = 0; i < r; i++){
			cin>>a[i];
			// cerr<<a[i]<<"\n";
        }		
    for (long i = 0; i < r; i++){
    	string s=a[i];
    	for (long j=0;j<c;j++){
    		if (s[j]!='S'){
    			continue;
    		}
    		if (j>0){
    			
				if (s[j-1]=='W'){
					cout<<"No";
					return(0);
				}
				else if (s[j-1]=='.'){
					a[i][j-1]='D';
				}
			}
			if (j<c-1){
				if (s[j+1]=='W'){
					// cerr<<"me";
					cout<<"No"<<endl;
					return(0);
				}
				else if (s[j+1]=='.'){
					a[i][j+1]='D';
				}

			}
			if (i>0){
				if (a[i-1][j]=='W'){
					cout<<"No";
					return 0;
				}
				else if (a[i-1][j]=='.'){
					a[i-1][j]='D';
				}
			}
			if (i<r-1){
				if (a[i+1][j]=='W'){
					cout<<"No";
					return 0;
				}
				else if (a[i+1][j]=='.'){
					a[i+1][j]='D';
				}

			}

        }
        // cerr<<2<<"\n";			

        }
        cout<<"Yes";
        for (long i = 0; i < r; i++){
        	// cerr<<a[i]<<"\n";
        	cout<<"\n"<<a[i];
        	

        }			

        }							
    


Comments

Submit
0 Comments
More Questions

742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing